package com.shujia.mr.stu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.net.URISyntaxException;

public class Demo05CustomPartitioner {
    // Mapper
    public static class MyMapper extends Mapper<LongWritable, Text, MyComparable, NullWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, MyComparable, NullWritable>.Context context) throws IOException, InterruptedException {
            // 1500100001,施笑槐,22,女,文科六班|406
            String[] split = value.toString().split("[,|]");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String gender = split[3];
            String clazz = split[4];
            String sumScore = split[5];
            context.write(
                    new MyComparable(id, name, Integer.parseInt(age), gender, clazz, Integer.parseInt(sumScore))
                    , NullWritable.get()
            );
        }
    }

    // Reducer

    // Driver
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException, URISyntaxException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 配置Job
        job.setJobName("Demo05CustomPartitioner");
        job.setJarByClass(Demo05CustomPartitioner.class);

        // 使用自定义分区
        job.setPartitionerClass(AgePartitioner.class);

        // 配置Mapper
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(MyComparable.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 设置Reduce数量
        job.setNumReduceTasks(4);

        // 验证args的长度
        if (args.length != 2) {
            System.out.println("请传入输入输出目录！");
            return;
        }

        String input = args[0];
        String output = args[1];

        // 配置输入输出的路径
        // 学生总分数据
        FileInputFormat.addInputPath(job, new Path(input));

        Path ouputPath = new Path(output);
        // 通过FileSystem来实现覆盖写入
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(ouputPath)) {
            fs.delete(ouputPath, true);
        }
        // 该目录不能存在，会自动创建，如果已存在则会直接报错
        FileOutputFormat.setOutputPath(job, ouputPath);

        // 启动任务
        // 等待任务的完成
        job.waitForCompletion(true);


    }
}

// 自定义排序
class MyComparable implements WritableComparable<MyComparable> {

    String id;
    String name;
    Integer age;
    String gender;
    String clazz;
    Integer sumScore;

    public MyComparable() {
    }

    public MyComparable(String id, String name, Integer age, String gender, String clazz, Integer sumScore) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.clazz = clazz;
        this.sumScore = sumScore;
    }

    @Override
    public int compareTo(MyComparable o) {
        String thisClazz = this.clazz;
        String thatClazz = o.clazz;

        Integer thisClz = 0;
        Integer thatClz = 0;

        switch (thisClazz.substring(2,3)){
            case "一":
                thisClz = 1;
                break;
            case "二":
                thisClz = 2;
                break;
            case "三":
                thisClz = 3;
                break;
            case "四":
                thisClz = 4;
                break;
            case "五":
                thisClz = 5;
                break;
            case "六":
                thisClz = 6;
                break;

        }

        switch (thatClazz.substring(2,3)){
            case "一":
                thatClz = 1;
                break;
            case "二":
                thatClz = 2;
                break;
            case "三":
                thatClz = 3;
                break;
            case "四":
                thatClz = 4;
                break;
            case "五":
                thatClz = 5;
                break;
            case "六":
                thatClz = 6;
                break;

        }

        String clz01 = thisClazz.substring(0, 1);
        String clz02 = thatClazz.substring(0, 1);

        Integer thisSumScore = this.sumScore;
        Integer thatSumScore = o.sumScore;
        if (thisClazz.equals(thatClazz)) {
            return thatSumScore.compareTo(thisSumScore);
        } else {
            if(clz01.equals(clz02)){
                return thisClz.compareTo(thatClz);
            }else {
                return thisClazz.compareTo(thatClazz);
            }
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(id);
        out.writeUTF(name);
        out.writeInt(age);
        out.writeUTF(gender);
        out.writeUTF(clazz);
        out.writeInt(sumScore);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        id = in.readUTF();
        name = in.readUTF();
        age = in.readInt();
        gender = in.readUTF();
        clazz = in.readUTF();
        sumScore = in.readInt();
    }

    @Override
    public String toString() {
        return id + "," + name + "," + age + "," + gender + "," + clazz + "," + sumScore;
    }
}

// 自定义分区
class AgePartitioner extends Partitioner<MyComparable, NullWritable> {

    @Override
    public int getPartition(MyComparable key, NullWritable value, int numPartitions) {
        int partition = 0;
        switch (key.age) {
            case 22:
                partition = 1;
                break;
            case 23:
                partition = 2;
                break;
            case 24:
                partition = 3;
                break;
        }
        return partition;
    }
}
